import 'package:flutter/material.dart';
import 'package:hao_ke/pages/utils/string_utils.dart';
import 'package:hao_ke/utils/dio_http.dart';
import 'package:hao_ke/widgets/my_button_widget.dart';
import 'package:hao_ke/widgets/title_content.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';
import '../auth/auth.dart';
import '../utils/scoped_model_helper.dart';

class InfoDetails extends StatefulWidget {
  final String pkid;

  const InfoDetails(this.pkid, {super.key});

  @override
  State createState() {
    return _InfoDetails();
  }
}

class _InfoDetails extends State<InfoDetails> {
  String image = "";
  String title = "";
  String content = "";
  String description = "";
  double price = 0;
  double originPrice = 0;
  String period = '';
  String isShow = "";
  String available = "";
  String canBook = "";
  bool loading = false;

  Widget buttons = Container(
    decoration: BoxDecoration(
      color: const Color.fromRGBO(182, 180, 180, 0.2),
      borderRadius: BorderRadius.circular(10),
    ),
    margin: const EdgeInsets.only(top: 40),
    child: Wrap(
      spacing: 5,
      children: [
        MyButtonWidget(title: '护理师', onPressed: () {}),
      ],
    ),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        centerTitle: true,
        title: const Text('项目详情'),
      ),
      body: Container(
        decoration: const BoxDecoration(
            gradient: LinearGradient(
                begin: Alignment.topCenter,
                end: Alignment.bottomCenter,
                colors: [
              Color.fromRGBO(200, 100, 100, 0.5),
              Color.fromRGBO(0, 0, 200, 0.5),
            ])),
        child: Container(
          decoration: BoxDecoration(
            color: const Color.fromRGBO(200, 200, 200, 0.5),
            borderRadius: BorderRadius.circular(10),
          ),
          margin: const EdgeInsets.all(10),
          padding: const EdgeInsets.all(20),
          child: Stack(
            alignment: AlignmentDirectional.center,
            children: [
              Column(
                children: [
                  TDImage(
                    height: 200,
                    width: 999,
                    imgUrl: image,
                    loadingWidget: const TDText(
                      '加载中。。。',
                    ),
                    errorWidget: const TDText(
                      '加载失败',
                    ),
                  ),
                  Container(
                    alignment: Alignment.center,
                    padding: const EdgeInsets.all(10),
                    margin: const EdgeInsets.only(top: 20),
                    width: 9999,
                    decoration: BoxDecoration(
                      color: const Color.fromRGBO(200, 200, 200, 0.3),
                      borderRadius: BorderRadius.circular(10),
                    ),
                    child: Column(
                        children: [
                          Text(title,style: const TextStyle(fontSize: 20,fontWeight: FontWeight.bold),maxLines: 1,overflow: TextOverflow.ellipsis,),
                          TitleContent('价格', price),
                          TitleContent('原价', originPrice),
                          TitleContent('时长', period),
                          TitleContent('是否展示', StringUtils.isOrNot(isShow)),
                          TitleContent('是否可用', StringUtils.isOrNot(available)),
                          TitleContent('可否预约', StringUtils.isOrNot(canBook)),
                          TitleContent('描述', description),
                        ]
                    ),
                  ),
                  Container(
                    margin: const EdgeInsets.only(top: 20),
                    child: Wrap(
                      spacing: 5,
                      children: [
                        MyButtonWidget(title: '护理师', onPressed: () {
                          Navigator.of(context).pushNamed('/project-employee/${widget.pkid}');
                        }),
                      ],
                    ),
                  )
                ]
              ),
              loading?TDLoading(size: TDLoadingSize.large,icon: TDLoadingIcon.circle,):Container()
            ],
          ),
        ),
      ),
    );
  }

  @override
  void initState() {
    super.initState();
    WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
      _getData();
    });
  }

  Future<void> _getData() async {
    setState(() {
      loading = true;
    });
    var response = await DioHttp()
        .init(context, ScopedModelHelper.getModel<AuthModel>(context).token)
        .get("/oms/project/detail/${widget.pkid}");
    setState(() {
      title = response['name'];
      image = response['image'];
      price = response['price'];
      originPrice = response['originPrice'];
      description = response['instruction'];
      period = response['period'].toString();
      isShow = response['isShow'].toString();
      available = response['available'].toString();
      canBook = response['canBook'].toString();
      loading = false;
    });
  }
}
